<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://www.yaodian100.com/jsp/yaodian100" prefix="yaodian100" %>
<%@ include file="/taglibs.jsp"%>
<!-- 数字验证 -->

<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>		
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<!-- 時間 -->	
<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>	
<s:actionerror cssClass="errmsg"/>
<s:actionmessage cssClass="errmsg"/>
<br/>
  <h1>编辑资料 - 批次新增上架檔</h1>
  <div class="batchstep">
    <div class="batchstep-2">
      <span class="step1">Step 1：查寻商品/选取商品</span>
      <span class="onstep2">Step 2：编辑资料</span>
      <span class="step3">Step 3：核对资料/完成编辑</span>
    </div>
  </div>
<script>
$().ready( function() {
	$('#startDateParam').datepick({minDate: new Date(), showOn: 'both',numberOfMonths:2,
    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	$('#endDateParam').datepick({minDate: new Date(), showOn: 'both', numberOfMonths:2,
    	buttonImageOnly: true, beforeShow:function(input, inst){return customRange('startDateParam');}, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	function customRange (x){
		//var startDt = $('#startDateParam').val(); //起始日
		var startDt = $('#'+x).val(); //起始日
		var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
		return {minDate:endDt}; 
	}
	$("#timestart").timePicker({step:60, startTime:"00:00", endTime:"23:00"});
	$("#timeend").timePicker({step:60, startTime:"00:59", endTime:"23:59"});
	
	$("input[name=pid]").each(function(index){
		var key = $(this).val();

		$('#startDate_'+key).datepick({minDate: new Date(), showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$('#endDate_'+key).datepick({minDate: new Date(), showOn: 'both', numberOfMonths:2,
	    	buttonImageOnly: true, beforeShow:function(input, inst){return customRange('startDate_'+key);}, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		
		$("#startTime_"+key).timePicker({step:60, startTime:"00:00", endTime:"23:00"});
		$("#endTime_"+key).timePicker({step:60, startTime:"00:59", endTime:"23:59"});
	});
	
	$('input[id^=shelfPrice_]').numeric({allow:"."});
	$('input[id^=shelfPriceParam]').numeric({allow:"."});

	$("#mainSubmit").click(function(){
		if ( "" == $("#MultiFile8_F1").val()){
			alert("尚未选择上传档案，请重新选择！");
			return false;
		}
	});
	$("#shelfPriceCal").change(function(){
		if($(this).val()!='4'){
			$("#pricetype4").hide();
			$("#pricetype123").show();
			$("#shelfPriceParam").val("0");
		}else{
			$("#pricetype123").hide();
			$("#pricetype4").show();
			
				
			
			
		}
			
	});
	$("#operators").change(function(){
		if($(this).val()=='1'||$(this).val()=='2'){			
			$("#shelfPriceParam").val("0");
		}else if($(this).val()=='3'||$(this).val()=='4'){
			$("#shelfPriceParam").val("1");
		}
			
	});
	$("#shelfPromoNM").change(function(){
		if($(this).val()=='1'){			
			$("#nametype1").show();
			$("#nametype2").hide();
		}else{
			$("#nametype2").show();
			$("#nametype1").hide();
		}
			
	});
	
	$("#submit2").click(function() {
		//alert($("input[name=pid]").length);
		var error = 0;
		if($("#shelfPromoNM").val()=="1"){
			if($("#promoteNameParam").val().length>20){
				$("#wordCountError").show();
				error++;
			}else{
				$("#wordCountError").hide();
			}
		}
		if($.trim($("#startDateParam").val())==""||$.trim($("#timestart").val())==""){		
			$("#onTimeError").show();
			error++;
		}else{
			$("#onTimeError").hide();
		}
		if($.trim($("#endDateParam").val())==""||$.trim($("#timeend").val())==""){		
			$("#offTimeError").show();
			error++;
		}else{
			$("#offTimeError").hide();
		}
		if ( !dateDiff($("#startDateParam").val() , $("#timestart").val() ,$("#endDateParam").val()  , $("#timeend").val())){
			$("#offTimeEarlyError").show();
			error++;		
		}else{
			$("#offTimeEarlyError").hide();
		}
		if($("#shelfPriceCal").val()=="4"){
			if($.trim($("#shelfPriceParam2").val())=="" || $.trim($("#shelfPriceParam2").val())=="请输入价格"){		
				$("#shelfPriceError").show();
				error++;
			}else{
				$("#shelfPriceError").hide();
			}
		}else{
			if($.trim($("#shelfPriceParam").val())=="" || $.trim($("#shelfPriceParam").val())=="请输入价格"){		
				$("#shelfPriceError").show();
				error++;
			}else{
				$("#shelfPriceError").hide();
			}
		}

		if(error>0){
			return;
		}
		
		$('input[id^="startDate_"]').attr("value", $("#startDateParam").val());
		$('input[id^="startTime_"]').attr("value", $("#timestart").val());
		$('input[id^="endDate_"]').attr("value", $("#endDateParam").val());
		$('input[id^="endTime_"]').attr("value", $("#timeend").val());
		
		
		//商品促銷小標
		if($("#shelfPromoNM").val()=="1"){
			$('input[id^="promoteName_"]').attr("value", $("#promoteNameParam").val());
		}else{
			$("input[name=pid]").each(function(index){
				var key = $(this).val();
				$("#promoteName_"+key).val($("#oldPromoName_"+key).val());
			});
		}
		//上架售價四則運算
		if($("#shelfPriceCal").val()=="4"){
			if($("#shelfPriceParam2").val()=='请输入价格'){
				$('input[id^="shelfPrice_"]').attr("value", "0");
			}else{
				$('input[id^="shelfPrice_"]').attr("value", $("#shelfPriceParam2").val());
			}
		}else{
			 $("input[name=pid]").each(function(index){
				var key = $(this).val();
				var baseprice = 0;
				var operatorprice = parseFloat($("#shelfPriceParam").val());
				var newprice =0;
				if($("#shelfPriceCal").val()=="1"){
					baseprice = parseFloat($("#price1_"+key).val());
				}else if($("#shelfPriceCal").val()=="2"){
					baseprice = parseFloat($("#price2_"+key).val());
				}else if($("#shelfPriceCal").val()=="3"){
					baseprice = parseFloat($("#price3_"+key).val());
				}
				
				if($("#operators").val()=="1"){//加
					newprice = baseprice + operatorprice;
				}else if($("#operators").val()=="2"){//減
					newprice = baseprice - operatorprice;
				}else if($("#operators").val()=="3"){//乘
					newprice = baseprice * operatorprice;
				}else if($("#operators").val()=="4"){//除
					newprice = Math.floor(baseprice / operatorprice*10)/10;
				}
				
				$("#shelfPrice_"+key).val(newprice);
					
			});
				
		}
			
		if(error==0){
			$("#dataoldornew").val("new");
			$("#oldsubmit").attr("disabled",true); 
			$("#nextsubmit").hide();
			$("#errtable").hide();	
			$("#download").submit();
		}
	});
	
	$("#oldsubmit").click(function() {
		$("#dataoldornew").val("old");
		$("#newsubmit").attr("disabled",true); 
		$(".olddis").attr("disabled",true); 
		$("#nextsubmit").hide();
		$("#errtable").hide();	
		$("#download").submit();
		
	});
	 
});

//日期判断,计算天数差的函数
function dateDiff(sDate1,sTime1, sDate2,sTime2){ //sDate1和sDate2是2002-12-18格式
	 var startDate = new Date(sDate1.split("-")[0],eval(sDate1.split("-")[1])-1,sDate1.split("-")[2],sTime1.split(":")[0],sTime1.split(":")[1],00); 
	 var endDate = new Date(sDate2.split("-")[0],eval(sDate2.split("-")[1])-1,sDate2.split("-")[2],sTime2.split(":")[0],sTime2.split(":")[1],00);
 if ( startDate >= endDate )return false;
 else return true;
}
function sumbitF(){
	var error = 0;
	$("input[name=pid]").each(function(index){
			var key = $(this).val();
			if($.trim($("#promoteName_"+key).val()).length>20){		
				$("#wordCountError_"+key).show();
				error++;
			}else{
				$("#wordCountError_"+key).hide();
			}
			if($.trim($("#startDate_"+key).val())==""||$.trim($("#startTime_"+key).val())==""){		
				$("#onTimeError_"+key).show();
				error++;
			}else{
				$("#onTimeError_"+key).hide();
			}
			if($.trim($("#endDate_"+key).val())==""||$.trim($("#endTime_"+key).val())==""){		
				$("#offTimeError_"+key).show();
				error++;
			}else{
				$("#offTimeError_"+key).hide();
			}
			if ( !dateDiff($("#startDate_"+key).val() , $("#startTime_"+key).val() ,$("#endDate_"+key).val()  , $("#endTime_"+key).val())){
				$("#offTimeEarlyError_"+key).show();
				error++;		
			}else{
				$("#offTimeEarlyError_"+key).hide();
			}
			if($.trim($("#shelfPrice_"+key).val())=="" || $.trim($("#shelfPrice_"+key).val())=="请输入价格"){		
				$("#shelfPriceError_"+key).show();
				error++;
			}else{
				$("#shelfPriceError_"+key).hide();
				if (isNaN($.trim($("#shelfPrice_"+key).val()))) {
					$("#shelfPriceFormatError_"+key).show();
					error++;
				}else{
					$("#shelfPriceFormatError_"+key).hide();
				}
			}				
	});
	if(error>0){
		return;
	}else{
		$("#download").submit();
	}
}
function hiddenSpan(pId) {
	
}
</script>
  <!-- div class="data-form"> -->
	<s:form action="downloadFile.do" id="download" method="post">
	<s:hidden name="paramBatchEventId"/>
	 <img src="/images/ecadmin/bianjiziliaoliucheng.jpg"></img>
	<div class="tipmsg" style="width:53%"><ul><li>系统会将你所选定的商品自动生成Excel报表，请在信息编辑完成后，下载报表。<br/>此阶段具有时效性，请在<Strong>25分钟</Strong>内完成编辑，并上传相关Excel报表</li></ul></div>
     <h2>下载报表</h2>		 
		<table class="tb-edit" style="width:53%">
          <tbody><tr>
          	<th>商品促銷小標</th><td>
            <select id="shelfPromoNM" name="shelfPromoNM" class="olddis">
              <option value="1">重新设定小标</option>
              <option value="2" selected>沿用现有小标</option>
            </select>
            <span id="nametype1" style="display:none"><input type="text" name="promoteNameParam" id="promoteNameParam" size="25" maxlength="20" value="">
            <span class="txt-gy">上限20个汉字</span>
            <div class="errmsg" id="wordCountError" style="display:none"><ul><li><span>商品促銷小標超过上限20个汉字</span></li></ul></div>
            </span>
            <span id="nametype2"><span class="txt-gy">将会套用各商品现有小标</span>
            </span>
           
            </td>
          </tr>
          <tr>
          	<th>*上架起始日期时间</th><td>
          	<input type="text"  class="olddis" name="startDateParam" id="startDateParam" size="20" value="<fmt:formatDate value="${shelf.startDate}" pattern="yyyy-MM-dd" />"/>	 
			<input id="timestart"  class="olddis" name="timestart" type="text" value="<fmt:formatDate value="${shelf.startDate}" pattern="HH:mm" />"  size="10"/>
            <div class="errmsg" id="onTimeError" style="display:none"><ul><li><span>上架起始日期时间为必填</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>*上架结束日期时间</th><td>
          	<input type="text"  class="olddis" name="endDateParam" id="endDateParam" size="20" value="<fmt:formatDate value="${shelf.endDate}" pattern="yyyy-MM-dd" />"/>	
			<input id="timeend"  class="olddis" name="timeend" type="text" value="<fmt:formatDate value="${shelf.endDate}" pattern="HH:mm" />" size="10"/>
			<div class="errmsg" id="offTimeError" style="display:none"><ul><li><span>上架结束日期时间为必填</span></li></ul></div>
			<div class="errmsg" id="offTimeEarlyError" style="display:none"><ul><li><span>上架結束日期不可大于上架起始日期</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>*上架售价</th><td>
            <select id="shelfPriceCal"  class="olddis">
              <option value="1">厂商建议售价</option>
              <option value="2">提报耀点价</option>
              <option value="3">商品成本</option>
              <option value="4" selected>直接输入价格</option>
            </select>
            <span id="pricetype4">
            <input type="text"  class="olddis" style="color:#aaa;" size="7" value="请输入价格" onblur="if(this.value=='') {this.value='请输入价格';this.style.color='#aaa';this.style.background='#FFDFDF'}" onfocus="if(this.value=='请输入价格') {this.value='';}this.style.color='#000';" id="shelfPriceParam2" name="shelfPriceParam2">
            </span>
            <span id="pricetype123" style="display:none">
            <select id="operators"  class="olddis">
              <option value="1">（+ 加）</option>
              <option value="2">（- 减）</option>
              <option value="3">（× 乘以）</option>
              <option value="4">（÷ 除以）</option>              
            </select>
            <input type="text" size="7"  class="olddis" value="请输入价格" onblur="if(this.value=='') {this.value='请输入价格';this.style.color='#aaa';this.style.background='#FFDFDF'}" onfocus="if(this.value=='请输入价格') {this.value='';}this.style.color='#000';" name="shelfPriceParam" id="shelfPriceParam" >
            </span>
            <span class="txt-gy">上架售价小数点1位数后无条件舍去</span>
            <div class="errmsg"  id="shelfPriceError" style="display:none"><ul><li><span>上架售价为必填</span></li></ul></div>
            </td>
          </tr>
          <tr>
	           <th></th>
	           <td><input type="button" id="submit2" value="全部套用，下载Excel报表 ">
	            <input type="hidden" id="dataoldornew" name="dataoldornew">
            <c:if test="${!empty event.modifyDate}"><input type="button" id="oldsubmit" value="下载之前上传的报表 "></c:if>
	           </td>
	      </tr>
        </tbody></table>

	  <div style="display:none">
	     <s:iterator value="detailMap" var="map" status="rows_index">
	          <input type="hidden" name="pid" value="${map.key}">>
	          <input type="hidden" id="price1_${map.key}" value="${map.value.dataOldMap['listPrice']}">
	          <input type="hidden" id="price2_${map.key}" value="${map.value.dataOldMap['prpsShelfPrice']}">
	          <input type="hidden" id="price3_${map.key}" value="${map.value.dataOldMap['productCost']}">
	          <s:textfield size="10"  name="detailMap['%{#map.key}'].dataNewMap['promoteName']" id="promoteName_%{#map.key}" value="%{#map.value.dataNewMap['promoteName']}" maxlength="20"/>
	           <input type="hidden" id="oldPromoName_${map.key}" value="${map.value.dataOldMap['promoteName']}">
	           <input type="text" name="startDate_${map.key}" id="startDate_${map.key}" size="8" value="${fn:substring(map.value.dataNewMap['startDate'],0,10)}"/>	 
			   <input type="text" name="startTime_${map.key}" id="startTime_${map.key}" size="4" value="${fn:substring(map.value.dataNewMap['startDate'],11,16)}"/>
	           <input type="text" name="endDate_${map.key}" id="endDate_${map.key}" size="8" value="${fn:substring(map.value.dataNewMap['endDate'],0,10)}"/>	
				<input type="text" name="endTime_${map.key}" id="endTime_${map.key}" size="4" value="${fn:substring(map.value.dataNewMap['endDate'],11,16)}"/>
	           <input type="text" name="shelfPrice_${map.key}" id="shelfPrice_${map.key}" size="8" value="${map.value.dataNewMap['shelfPrice']}"/>
	      </s:iterator>
  		</div>
     	</s:form>     	
      	<br />     	
      	 <h2>上传报表</h2>
      	 <div class="tipmsg"  style="width:53%"><ul><li>请将核对编辑后的报表上传。</li></ul></div>         
		<s:form action="uploadFile.do" method="post" enctype ="multipart/form-data">
	    	<table style="width:53%;clear:none;" border="0" align="left" class="tb-data">		
		        <tr>
		          	<td><input id="MultiFile8_F1" class="multi accept-xls" maxlength="1" type="file" name="upload"  accept="xls"/>
		          	<s:submit id="mainSubmit" value="上传Excel报表" /><br><br>
		         	<c:if test="${!empty step2Upload}">
			         	<div style="width:100%;font-size: 15px;" id="errtable" >
			         	上传成功${uploadSuccess}笔<br />
			         	上传失败${uploadErr}笔<br />
				         	<c:if test="${!empty errId}">
				         	上传失败的商品编号（最多显示20笔）:
					         	<c:forEach items="${errId}" var="errId" varStatus="rows_index">
					         	${errId} / 
					         	</c:forEach>	         		
				         	</c:if>
			         	</div>
		         	</c:if>
		         	</td>
		       </tr>
	    	 </table>
	   </s:form>
       <s:form action="confirm.do">
        <table width="560"  border="0"><tr height="40"><td>
     	 <br /><c:if test="${empty event.modifyDate}"><a href="changePage.do" class="pstep">﹤回上一步</a></c:if> 
      	<c:if test="${uploadSuccess>0 and uploadErr<=0}">
      		<input type="submit" id="nextsubmit" value="上传完成！下一步﹥" name="submit1" class="nstep" ><br />
		</c:if>
		</td></tr></table>
     </s:form>
    <!-- </div>  -->